package apps.softtech.fastmarket;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import apps.softtech.Utility.RequestDataFromServer;
import apps.softtech.fastmarket.model.DatabaseHandler;
import apps.softtech.fastmarket.model.UserInfo;

public class ActivityOfferTEST extends Activity{
	Dialog mDialog;
	AlertDialog.Builder mBuilder;
	/**
	 * idOffer
	 */
	int idOffer;
	/**
	 * price
	 */
	float mOffer;
	/**
	 * id product
	 */
	int mProductId=18;
//	final EditText etOffer ;
	/**
	 * send offer
	 */
	Button btOffer;
	/**
	 * database
	 */
	DatabaseHandler dbh = new DatabaseHandler(this);
	/**
	 *  UPDATE OFFER
	 */
	ArrayList<apps.softtech.fastmarket.model.Offer> mOffersList = new ArrayList<apps.softtech.fastmarket.model.Offer>();
	ArrayList<UserInfo> mUserInfosList = new ArrayList<UserInfo>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE); 
		setContentView(R.layout.search_no_result);
		new RegisterGoogleCM(ActivityOfferTEST.this).registerGCM();
		 
		TextView tv = (TextView)findViewById(R.id.tvNoResult);
		tv.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				mDialog.show();
			}
		});
		mDialog = new Dialog(ActivityOfferTEST.this);
		mDialog.setContentView(R.layout.dialog_offer);
		mDialog.setTitle(getResources().getString(R.string.offer_price));
		final EditText etOffer = (EditText)mDialog.findViewById(R.id.etOffer);
		 etOffer.requestFocus();
		 btOffer = (Button)mDialog.findViewById(R.id.btOffer);
		btOffer.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (etOffer.getText().toString().trim().equals("")
						|| etOffer.getText().toString().trim().equals(null)) {
					return;
				}
					mOffer = Float.parseFloat(etOffer.getText().toString()
							.trim());
					if (mOffer > 0) {
						Offer();
					}
			}

			
		});
		
		
	}
	/**
	 * offer
	 */
	private void Offer() {
		new AsyncTaskOffer().execute();
	}
	/**
	 * remove offer
	 *
	 */
	private void removeOffer(){
		new AsyncTaskRemoveOffer().execute();
	}
	/**
	 * update interface offer when have new offer
	 */
	private void updateViewOffer(){
		if(isOffer(true)){
			
		}
	}
	/**
	 * is offer
	 */
	private boolean isOffer(boolean offer){
		return offer;
	}
	
	/**
	 * offer
	 *
	 */
	class AsyncTaskOffer extends AsyncTask<Void, Void, Void>{
		ProgressDialog pd;
		int success;
		@Override
		protected void onPreExecute() {
			pd = new ProgressDialog(ActivityOfferTEST.this);
			pd.setMessage("Offering.....");
			pd.show();
			super.onPreExecute();
		}
		@Override
		protected Void doInBackground(Void... params) {
			try {
				JSONObject json = new JSONObject(new RequestDataFromServer().requestOffer(mProductId, mOffer));
				success = json.getInt("success");
			} catch (JSONException e) {
				e.printStackTrace();
			}
			return null;
		}
		@Override
		protected void onPostExecute(Void result) {
			if(success==1){
				Toast.makeText(ActivityOfferTEST.this, "You are offer.", Toast.LENGTH_LONG).show();
				mDialog.dismiss();
			}else{
				Toast.makeText(ActivityOfferTEST.this, "Not offer.", Toast.LENGTH_LONG).show();
			}
			pd.dismiss();
			super.onPostExecute(result);
		}
		
	}
	/**
	 * remove offer
	 */
	class AsyncTaskRemoveOffer extends AsyncTask<Void, Void, Void>{
		ProgressDialog pd;
		int success;
		@Override
		protected void onPreExecute() {
			pd = new ProgressDialog(ActivityOfferTEST.this);
			pd.setMessage("Offering.....");
			pd.show();
			super.onPreExecute();
		}
		@Override
		protected Void doInBackground(Void... params) {
			try {
				JSONObject json = new JSONObject(new RequestDataFromServer().requestRemoveOffer(idOffer));
			success = json.getInt("success");
			} catch (JSONException e) {
				e.printStackTrace();
			}
			return null;
		}
		@Override
		protected void onPostExecute(Void result) {
			if(success==1){
				Toast.makeText(ActivityOfferTEST.this, "You are remove offer.", Toast.LENGTH_LONG).show();
			}else{
				Toast.makeText(ActivityOfferTEST.this, "Not remove offer.", Toast.LENGTH_LONG).show();
			}
			pd.dismiss();
			super.onPostExecute(result);
		}
		
	}
	/**
	 * user info offer
	 */
	class AsyncTaskUserOffer extends AsyncTask<Void, Void, Void>{
		ProgressDialog pd;
		int success;
		@Override
		protected void onPreExecute() {
			pd = new ProgressDialog(ActivityOfferTEST.this);
			pd.setMessage("Offering.....");
			pd.show();
			super.onPreExecute();
		}
		@Override
		protected Void doInBackground(Void... params) {
			try {
				
				JSONArray jsonData = new JSONArray();
				JSONObject jsonCategoryParentList = new JSONObject(new RequestDataFromServer().requestUserOfferToProduct(mProductId));
				int success = jsonCategoryParentList.getInt("success");
				if(success==1){
					jsonData = jsonCategoryParentList.getJSONArray("data");
					for(int i=0;i<jsonData.length();i++){
						JSONObject jsonList = new JSONObject(jsonData.getJSONObject(i).toString());
						apps.softtech.fastmarket.model.Offer offer = new apps.softtech.fastmarket.model.Offer();
						offer.setCreatedDate(jsonList.getString("st_CreatedDate"));
						offer.setOfferPrice(jsonList.getString("st_OfferPrice"));
						offer.setUserId(jsonList.getInt("st_UserId"));
						dbh.insertOffer(offer);
						mOffersList.add(offer);
					}
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
			return null;
		}
		@Override
		protected void onPostExecute(Void result) {
			if(mOffersList.size()!=0){
				for(int i=0;i<mOffersList.size();i++){
					int idUser = mOffersList.get(i).getUserId();
					new AsyncTaskUser().execute(idUser);
				}
			}
			pd.dismiss();
			super.onPostExecute(result);
		}
		
	}
	/**
	 * get User
	 */
	class AsyncTaskUser extends AsyncTask<Integer, Void, Void>{

		@Override
		protected Void doInBackground(Integer... params) {
try {
				
				JSONObject jsonList = new JSONObject();
				JSONObject jsonCategoryParentList = new JSONObject(new RequestDataFromServer().requestUserInfoById(params[0]));
				int success = jsonCategoryParentList.getInt("success");
				if(success==1){
						jsonList = jsonCategoryParentList.getJSONObject("data");
						UserInfo ui = new UserInfo();
						ui.setAvatarPath(jsonList.getString("st_AvatarPath"));
						ui.setEmail(jsonList.getString("st_Email"));
						ui.setCreatedDate(jsonList.getString("st_CreatedDate"));
						ui.setRate(jsonList.getInt("st_Rate"));
						dbh.insertUserInfo(ui);
						mUserInfosList.add(ui);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
			return null;
		}
		
	}

}
